import csv
from objs1 import Student

stus = [
    Student(no="001",name='张三',age=23,gender='男',test='abc'),
    Student(no="002",name='张三',age=23,gender='男'),
    Student(no="003",name='张三',age=23,gender='男',test='ddd'),
    Student(no="004",name='张三',age=23,gender='男'),
    Student(no="005",name='张三',age=23,gender='男'),
    Student(no="006",name='张三',age=23,gender='男'),
]
#如果要用csv来写对象，需要将对象转换为字典
stu_dicts = [
    {'no':s.no,'name':s.name,'age':s.age,'gender':s.gender,'test':s.test} for s in stus
]
with open('stu.csv','w',encoding='utf-8',newline='') as f:
    fname = [s for s in stu_dicts[0].keys()] #列表生成式
    writer = csv.DictWriter(f,fieldnames=fname)
    writer.writeheader()
    writer.writerows(stu_dicts)